/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.ptitwrap;

import android.text.InputFilter;
import android.util.Log;
import android.widget.EditText;

/**
 * A helper class with static methods (baaah)
 * 
 * @author Pierre-Yves Ricau
 * 
 */
public class ViewHelper {

	private static final String	TAG	= ViewHelper.class.getSimpleName();

	/**
	 * Extracts the input from the EditText and returns its int value.
	 * 
	 * The input should ALWAYS be numbers (the EditText must be properly set
	 * with the right {@link InputFilter}s)
	 */
	public static int inputAsInt(EditText input) {

		String stringValue = input.getText().toString();

		if (stringValue.equals("")) {
			return 0;
		}

		try {
			return Integer.parseInt(stringValue);
		} catch (NumberFormatException e) {
			Log.e(TAG, "Could not parse input [" + stringValue + "] as a number. Please set your EditText with the right InputFilter", e);
			return 0;
		}
	}

}
